package com.aiju.common.http.interceptor;


import com.aiju.common.http.AiJuHttp;
import com.aiju.common.http.AiJuHttpProxy;
import com.aiju.support.aop.AProxyFactory;
import com.aiju.support.aop.IInterceptor;
import com.aiju.support.aop.IInterceptorHandler;
import com.aiju.widgit.dialog.DialogHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: 佐为
 * @date: 2017-03-21 17:09
 * @version: V1.0
 * @Description: Http代理工厂
 */
public class HttpFactory extends AProxyFactory<AiJuHttp> {

    private ArrayList interceptors = new ArrayList();

    private HttpHandler mHttpHandler;
    private ProgressDialogInterceptor mDialogInterceptor;

    private static volatile HttpFactory instance;

    public static HttpFactory getInstance() {
        if (instance == null) {
            synchronized (HttpFactory.class) {
                if (instance == null) {
                    instance = new HttpFactory();
                }
            }
        }
        return instance;
    }

    private HttpFactory() {
        mHttpHandler = new HttpHandler();
        mHttpHandler.registerMethods();
        mDialogInterceptor = new ProgressDialogInterceptor();

        //网络拦截
        interceptors.add(new NetWorkInterceptor());
        //重复请求拦截
        interceptors.add(new RepeatInterceptor());
        //不拦截，加入加载对话框
        interceptors.add(mDialogInterceptor);
        //不拦截，加入打印参数、返回值、耗时日志
        interceptors.add(new HttpLogInterceptor());
    }

    public void setDialogHelper(DialogHelper dialogHelper) {
        if (mDialogInterceptor != null) {
            mDialogInterceptor.setDialogHelper(dialogHelper);
        }
    }

    @Override
    public AiJuHttp createProxied() {
        return  AiJuHttpProxy.getInstance();
    }

    @Override
    public IInterceptorHandler createInterceptorHandler() {
        return mHttpHandler;
    }

    @Override
    public List<IInterceptor> createInterceptors() {
        return interceptors;
    }
}
